<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPower(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">

          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="editPower(row.type+1,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editPermission(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPower(row.id)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-card>

      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showAddDialog" title="弹层标题" @close="showAddDialog = false">
        <!-- 表单内容 -->
        <el-form label-width="100px" :model="formData">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <template>
              <el-switch
                v-model="formData.enVisible"
                active-value="1"
                inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="cancelPower">取消</el-button>
            <el-button type="primary" @click="submitAdd">确定</el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { listToTree } from '@/utils'
// import { reqGetPersonalDetail } from '@/api/employees'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showAddDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }

    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await reqGetPermissionList()
      this.permissionList = listToTree(res, '0')
    },
    addPower(type, pid) {
      console.log(type, pid)
      this.showAddDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    editPower(type, pid) {
      console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
      this.showAddDialog = true
    },
    async submitAdd() {
      try {
        await this.$confirm('确认吗', '温馨提示')
      } catch {
        return this.$message.success('取消成功')
      }
      if (!this.formData.id) {
        await reqAddPermission(this.formData)
      } else {
        await reqUpdatePermission(this.formData)
      }
      this.$message.success('操作成功')
      this.showAddDialog = false
      this.getPermissionList()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    async delPower(id) {
      try {
        await this.$confirm('确认删除吗', '温馨提示')
      } catch {
        return this.$message.success('取消成功')
      }
      console.log(id)
      await reqDelPermission(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    },
    async editPermission(id) {
      console.log(id)
      this.showAddDialog = true
      const res = await reqGetPermissionDetail(id)
      console.log(res)
      this.formData = res
    },
    cancelPower() {
      this.showAddDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  }
}
</script>
